package com.windy.search.service.impl;

import com.windy.api.product.ProductClient;
import com.windy.app.entity.Product;
import com.windy.app.vo.Result;
import com.windy.search.entity.ESProduct;
import com.windy.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("searchService")
public class SearchServiceImpl implements SearchService {

    @Resource
    private ProductClient productClient;

    @Autowired
    private ElasticsearchRestTemplate template;

    @Override
    public Result initProduct() {
        List<Product> productList = productClient.findAllProduct();
        productList.forEach(product->{
            ESProduct esProduct = new ESProduct();
            esProduct.setId(product.getId());
            esProduct.setName(product.getName());
            esProduct.setPic(product.getPic());
            template.save(esProduct);
        });
        return new Result(true,"同步成功");
    }

    @Override
    public Result product2es(Product product) {
        ESProduct esProduct = new ESProduct();
        esProduct.setId(product.getId());
        esProduct.setName(product.getName());
        esProduct.setPic(product.getPic());
        template.save(esProduct);
        return new Result(true,"同步成功");
    }
}
